package com.alibaba.citrus.cr.mn.order.center.facade.repository.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.function.assemble.impl.salesorder.SalesOrderCreateBizAssembleOfMemberCenter;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.OrderLinePageQueryRequestToPageQueryLineESRequestConvertor;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.OrderLineIndexToOrderLineSDOConvertor;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.PageQuerySaleOrderDetailCovert;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crb2btradecenter.model.dto.PageQueryOrderLineESRequest;

import com.alibaba.citrus.cr.mn.order.center.facade.repository.OrderLineQueryRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crb2btradecenter.model.dto.PageQuerySaleOrderDetailGroupRequest;
import com.epoch.app.crb2btradecenter.obtcsaleorderlineindex.model.ObtcSaleOrderLineIndex;
import com.epoch.app.crb2btradecenter.obtcsaleorderlineindex.service.ObtcSaleOrderLineIndexService;
import com.epoch.app.mnordercenter.model.dto.OrderLineRepoQueryReq;
import com.epoch.app.mnordercenter.model.dto.OrderLineSDO;
import com.epoch.app.mnordercenter.model.dto.SalesOrderAtomPageQueryRequest;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @date 2023/5/23 20:39
 */
@Service("orderLineQueryRepositoryBiz")
public class OrderLineQueryRepositoryImpl implements OrderLineQueryRepository {

    private static final Log log = Log.getLogger(OrderLineQueryRepositoryImpl.class);

    @Resource
    private ObtcSaleOrderLineIndexService obtcSaleOrderLineIndexService;

    @Override
    public Result<List<OrderLineSDO>> pageQuery(OrderLineRepoQueryReq pageQueryRequest) {
        PageQueryOrderLineESRequest queryOrderLineRequest = OrderLinePageQueryRequestToPageQueryLineESRequestConvertor.convert(pageQueryRequest);
        Result<List<ObtcSaleOrderLineIndex>> response = obtcSaleOrderLineIndexService.loadOrderLineListES4OrderCenter(queryOrderLineRequest);
        List<OrderLineSDO> result = Nullable.stream(response.getResult()).map(OrderLineIndexToOrderLineSDOConvertor::convert).collect(Collectors.toList());
        return Result.listSuccess(result,response.getTotal());
    }

    @Override
    public Result<List<ObtcSaleOrderLineIndex>> loadDetailForPage(SalesOrderAtomPageQueryRequest request) {
        PageQuerySaleOrderDetailGroupRequest esRequest = PageQuerySaleOrderDetailCovert.covert(request);
        log.info("OrderLineQueryRepositoryImpl#loadDetailForPage esRequest:{}", JSON.toJSONString(esRequest));
        Result<List<ObtcSaleOrderLineIndex>> listResult = obtcSaleOrderLineIndexService.pageOrderLineList(esRequest);
        log.info("OrderLineQueryRepositoryImpl#loadDetailForPage listResult:{}", JSON.toJSONString(listResult));
        if (CollectionUtils.isNotEmpty(listResult.getResult())) {
            listResult.setSuccess(true);
        }
        return listResult;
    }


}
